﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using Agnes.Core;
using Caliburn.Micro;

namespace Agnes.TaskManager
{
    public class NewTaskSummaryCmd
    {
        private static NewTaskSummaryCmd _instance;

        /// <summary>
        /// 返回唯一实例
        /// </summary>
        public static NewTaskSummaryCmd Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new NewTaskSummaryCmd();
                }
                return _instance;
            }
        }

        private RoutedCommand _requery;
        /// <summary>
        /// 是否能执行
        /// </summary>
        public bool CanExcuted { get; set; }

        NewTaskSummaryCmd()
        {
            CanExcuted = false;
            _requery = new RoutedCommand("New Task Cmd", typeof(NewTaskSummaryCmd));
        }

        /// <summary>
        /// 浏览文章的路由命令
        /// </summary>
        public static RoutedCommand Requery
        {
            get
            {
                return Instance._requery;
            }
        }

        /// <summary>
        /// 浏览文章执行代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Excuted(object sender, ExecutedRoutedEventArgs e)
        {
            Task newTask = Task.CreateAsChild();
            TaskEditorViewModel taskEditorViewModel = new TaskEditorViewModel(newTask, false);
            IWindowManager windowManager = IoC.Get<IWindowManager>();

            newTask.BeginEdit();
            if (windowManager.ShowDialog(taskEditorViewModel) == true)
            {
                newTask.ApplyEdit();
                newTask.Save();
                TaskManagerService.TaskManager.SelectedTask.ParentTask.Childs.Add(newTask);
            }
            else
            {
                newTask.CancelEdit();
            }
        }

        /// <summary>
        /// 判断是否能执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void CanExcute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (TaskManagerService.TaskManager.SelectedTask == null
                || TaskManagerService.TaskManager.SelectedTask.ParentTask == null)
            {
                e.CanExecute = false;
            }
            else
            {
                e.CanExecute = true;
            }
        }
    }
}
